using System;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.Faces.Base;
using <<|=BOProjectName|>>;

namespace <<|=UIBaseProjectName|>>
{
    /// <summary>
    /// The ControlFactory for this particular project, to allow for dynamic creation of the controls in the project
    /// using a different ControlFactory for Win and VWG.
    /// </summary>
    public partial interface I<<|=ProjectName|>>ControlFactory : IControlFactory
    {
        I<<|=ControlName|>> Create<<|=ControlName|>>();
    }

    /// <summary>
    /// The Interface for the <<|=ControlName|>>, so that the control can be treated the same in Win and VWG.
    /// </summary>
    public interface I<<|=ControlName|>> : IControlHabanero, IFormControl
    {

    }

    /// <summary>
    /// The common code shared between the Win and VWG version of this control.  For any common behaviour, place code
    /// in this manager.  For any specific behaviour for a platform, place the code in the Win or VWG controls themselves.
    /// </summary>
    public class <<|=ControlName|>>Manager
    {
        private readonly I<<|=ControlName|>> _<<|=#ControlName|>>;
        private readonly I<<|=ProjectName|>>ControlFactory _controlFactory;

        /// <summary>
        /// Constructor that takes the control itself (which this object manages), and the Control factory for this project
        /// so that you can make compound controls using the other controls of this project.
        /// </summary>
        /// <param name="<<|=#ControlName|>>">The control itself</param>
        /// <param name="controlFactory">The factory used to create any controls that are to placed on this control</param>
        public <<|=ControlName|>>Manager(I<<|=ControlName|>> <<|=#ControlName|>>, I<<|=ProjectName|>>ControlFactory controlFactory)
        {
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");
            this._<<|=#ControlName|>> = <<|=#ControlName|>>;
            this._controlFactory = controlFactory;
        }

        public void Initialise()
        {
            // create the edtiable grid
            IEditableGridControl <<|=#ClassName|>>GridControl = _controlFactory.CreateEditableGridControl();
            
            // Used to specify a custom UI def
            <<|=#ClassName|>>GridControl.Initialise(ClassDef.Get<<<|=ClassName|>>>(), "<<|=UIDefName|>>");

            // load the collection of <<|=ClassName+|>>.  You can set the criteria and order by values here.
            BusinessObjectCollection<<<|=ClassName|>>> <<|=#ClassName|>>Collection = Broker.GetBusinessObjectCollection<<<|=ClassName|>>>("", "");
            <<|=#ClassName|>>GridControl.SetBusinessObjectCollection(<<|=#ClassName|>>Collection);

            // set this to true to enable the filters.  Then add your filters below (see commented out line)
            <<|=#ClassName|>>GridControl.FilterControl.Visible = false;
            //<<|=#ClassName|>>GridControl.FilterControl.AddStringFilterTextBox("", "");

            // add the grid to the control so that it fills the control.
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(_<<|=#ControlName|>>);
            layoutManager.AddControl(<<|=#ClassName|>>GridControl, BorderLayoutManager.Position.Centre);
        }
    }
}
